package com.grantheath.erebus.controller.transitionManagers.linearTransitionManagers
{
	import com.grantheath.erebus.model.enum.notifications.ErebusInitializationNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusPreloaderNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusPageNotifications;
	import com.grantheath.erebus.model.interfaces.IErebusURLProxy;
	import com.grantheath.erebus.model.interfaces.IErebusAssetLoaderProxy;
	import com.grantheath.erebus.model.interfaces.IErebusPageLoaderProxy;
	import com.grantheath.erebus.model.interfaces.IErebusSiteTreeProxy;
	import com.grantheath.erebus.model.interfaces.IErebusAssetNode;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;
	import com.grantheath.erebus.model.enum.names.ErebusProxyNames;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.interfaces.ICommand;

	/**
	 * @author grantheath
	 */
	public class ErebusTransitionInteruptedCommand extends SimpleCommand implements ICommand 
	{
		override public function execute(notification : INotification) : void
		{
			var siteTreeProxy : IErebusSiteTreeProxy = facade.retrieveProxy(ErebusProxyNames.SITE_TREE_PROXY) as IErebusSiteTreeProxy;			var urlProxy : IErebusURLProxy = facade.retrieveProxy(ErebusProxyNames.URL_PROXY) as IErebusURLProxy;
			var targetURL : String = urlProxy.URL;
			var pageNodeArrays : Array = new Array(siteTreeProxy.loadQueue, siteTreeProxy.displayQueue, siteTreeProxy.uninitializeQueue, siteTreeProxy.hideQueue);
			var pageNode : IErebusPageNode;
			var assetNode : IErebusAssetNode;
			
			trace("transition interupted\n\n");
			//cleanup loaded and undisplayed nodes
			for each(pageNode in siteTreeProxy.loadedPages){
				if(!siteTreeProxy.isDisplayed(pageNode)){
					siteTreeProxy.unregisterLoadedPageNode(pageNode);
				}
			}
			//clear event listeners on all pages and preloaders
			for each(var pageNodeArray : Array in pageNodeArrays){
				for each(pageNode in pageNodeArray){
					if(pageNode.page){
						var pageLoaderProxy : IErebusPageLoaderProxy = facade.retrieveProxy(ErebusProxyNames.PAGE_LOADER_PROXY) as IErebusPageLoaderProxy;
						var assetLoaderProxy : IErebusAssetLoaderProxy = facade.retrieveProxy(ErebusProxyNames.ASSET_LOADER_PROXY) as IErebusAssetLoaderProxy;
						
						sendNotification(ErebusPageNotifications.CLEAR_PAGE_TRANSITION_EVENTS, pageNode);
						sendNotification(ErebusPreloaderNotifications.CLEAR_PRELOADER_TRANSITION_EVENTS, pageNode);
						pageLoaderProxy.removePageLoadEvents(pageNode);
						for each(assetNode in pageNode.pageAssets){
							assetLoaderProxy.removeAssetLoadEvents(assetNode);
						}
					}
				}
			}
			siteTreeProxy.loadQueue = new Array();
			siteTreeProxy.uninitializeQueue = new Array();
			siteTreeProxy.displayQueue = new Array();
			siteTreeProxy.assetQueue = new Array();
			siteTreeProxy.interuptTransition = false;
			siteTreeProxy.isTransitioning = false;
			sendNotification(ErebusInitializationNotifications.INITIALIZE_QUEUES, targetURL);
		}
	}
}
